﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
using QUANLYCHAMCONG.BL.BUS;
using DgvFilterPopup;

namespace QUANLYCHAMCONG.PL.CMS.Danhmuc
{
    public partial class Ca : UserControl
    {
        public delegate void _Close();
        public event _Close Close;
        SHILF ca;
        int flag = 0;
        frmHoidap f;
        public Ca()
        {
            InitializeComponent();
        }
        private void Event()
        {

            txtmaca.Focus();
            this.toolbar1.Add += new Toolbar._Add(toolbar1_Add);
            this.toolbar1.Edit += new Toolbar._Edit(toolbar1_Edit);
            this.toolbar1.Save += new Toolbar._Save(Save);
            this.toolbar1.Delete += new Toolbar._Delete(toolbar1_Delete);
            this.toolbar1.Close += new Toolbar._Close(toolbar1_Close);
            this.toolbar1.Refresh += new Toolbar._Refesh(Refresh);
            InitGrid();
            LoadData();
        }

        void Refresh()
        {
            RemoveCheckbox();
            this.toolbar1.SetSave();
            this.flag = 0;
            this.dgvca.ReadOnly = true;
            this.dgvca.AllowUserToAddRows = true;
            LoadData();
            this.Clear();
        }
        void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvca.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvca.Columns.RemoveAt(0);
                this.dgvca.Controls.RemoveByKey(((CheckBox)this.dgvca.Controls.Find("checkboxHeader", true)[0]).Name);
                LoadData();
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvca.Columns.Insert(0, cb);
                Rectangle rect = this.dgvca.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width / 33;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvca.Controls.Add(checkboxHeader);
                this.dgvca.Columns[0].Width = 50;
                this.dgvca.DataSource = dt;
            }
        }

        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvca.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvca.RowCount; i++)
                {
                    this.dgvca[0, i].Value = ((CheckBox)this.dgvca.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvca.RowCount; i++)
                {
                    this.dgvca[0, i].Value = false;
                }
            }
        }
        void toolbar1_Delete()
        {
            flag = 3;
            this.dgvca.ReadOnly = false;
            this.dgvca.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
            AddCheckbox(SHILFBUS.SelectAll());

        }

        void toolbar1_Edit()
        {
            flag = 2;
            Clear();
            this.dgvca.Focus();
            this.dgvca.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgvca.Rows[0].Selected = true;
            this.dgvca.ReadOnly = false;
            this.dgvca.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
        }

        void toolbar1_Add()
        {
            flag = 1;
            Clear();
            CreateID();
            this.toolbar1.SetAdd_Edit_Delete();
            txttenca.Focus();

        }
        int TONGGIO(string begin, string last)
        { 
            //do du lieu thuoc dang hh:hh
            string gio1 = begin.Substring(0,2);
            string gio2 = last.Substring(0,2);
            string phut1 = begin.Substring(3, 2);
            string phut2 = last.Substring(3, 2);
            int phut = int.Parse(phut2) - int.Parse(phut1);
            int gio = int.Parse(gio2) - int.Parse(gio1);
            int tong = gio * 60+phut;
            return tong;

        }
        SHILF AddDuLieu(SHILF cas)
        {
            cas.MACA = txtmaca.Text;
            cas.TENCA = txttenca.Text;
            cas.TIMEIN = txtgiovao.Text;
            cas.TIMEOUT = txtgiora.Text;
            cas.GIOVAOTU = txtgiovaotu.Text;
            cas.GIOVAODEN = txtgiovaoden.Text;
            cas.GIORATU = txtgioratu.Text;
            cas.GIORADEN = txtgioraden.Text;
            cas.CHOPHEPDITRE = int.Parse( txtchopheptre.Text);
            cas.CHOPHEPVESOM = int.Parse(txtchophepsom.Text);
            cas.TONGGIO = TONGGIO(txtgiovao.Text, txtgiora.Text);
            cas.CONG = float.Parse(txtsocong.Text);
            if (cbditre.Checked == true)
                cas.TINHDITRE = true;
            else
                cas.TINHDITRE = false;
            if (cbvesom.Checked == true)
                cas.TINHVESOM = true;
            else
                cas.TINHVESOM = false;
            return cas;
        }
        void Save()
        {
            //khoi tao
            ca = new SHILF();
            if (flag == 1)
            {
                if (SHILFBUS.Insert(AddDuLieu(ca)) == true)
                {
                    f = new frmHoidap("Thêm ca làm việc thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
                else
                {
                    f = new frmHoidap("Thêm ca làm việc có lỗi xảy ra!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                    f.ShowDialog();
                    LoadData();
                    this.toolbar1.SetSave();
                    Clear();
                }
            }
            if (flag == 2)
            {
                try
                {
                    f = new frmHoidap("Bạn có muốn sửa ca làm việc  ?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        if (SHILFBUS.Update(AddDuLieu(ca)) == true)
                        {
                            f = new frmHoidap("Sửa ca làm việc thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                            LoadData();
                            this.dgvca.ReadOnly = true;
                            this.toolbar1.SetSave();
                            Clear();
                        }
                        else
                        {
                            f = new frmHoidap("Có lỗi khi Sửa ca làm việc !", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                            f.ShowDialog();
                            this.toolbar1.SetSave();
                            this.dgvca.ReadOnly = true;
                            Clear();
                        }
                    }
                    else
                    {
                        this.dgvca.ReadOnly = true;
                        this.toolbar1.SetSave();
                        Clear();
                    }
                }
                catch { return; }
            }
            if (flag == 3)
            {
                f = new frmHoidap("Bạn có chắc chắn muốn xóa dữ liệu ca làm việc vừa chọn?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.dauhoi);
                if (f.ShowDialog() == DialogResult.Yes)
                {
                    foreach (DataGridViewRow dr in this.dgvca.Rows)
                    {
                        try
                        {
                            if (dr.Cells[0].Value != null)
                            {
                                ca = new SHILF();
                                ca.MACA = dr.Cells["MACA"].Value.ToString();
                                SHILFBUS.Delete(ca);
                            }
                        }
                        catch
                        {
                            f = new frmHoidap("Bạn chưa chọn phần ca làm việcnào vui lòng chọn lại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                        }
                    }
                    f = new frmHoidap("Xóa thành công ca làm việc!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    RemoveCheckbox();
                    this.toolbar1.SetSave();
                    this.dgvca.ReadOnly = true;
                    this.dgvca.AllowUserToAddRows = true;
                }

            }

        }
        void CreateID()
        {
            ca = new SHILF();
            this.txtmaca.Text = SHILFBUS.GetID();
        }
        private void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Panel)
                {
                    foreach(Control s in c.Controls)
                    {
                        if (s is GroupBox)
                        {
                            foreach (Control d in s.Controls)
                                if (d is TextBox)
                                    d.Text = "";
                        }
                    }
                }
            }
            txtgiora.Text = "00:00";
            txtgiovao.Text = "00:00";
            txtgioratu.Text = "00:00";
            txtgioraden.Text = "00:00";
            txtgiovaotu.Text = "00:00";
            txtgiovaoden.Text = "00:00";
            cbditre.Checked = false;
            cbvesom.Checked = false;
            txtsocong.Text = "1.0";
        }
        void LoadData()
        {
            ca = new SHILF();
            Common.HightLightDataGridViewRow(ref this.dgvca, SHILFBUS.SelectAll());
            this.dgvca.Columns["MACA"].Width = 100;
            this.dgvca.Columns["TONGGIO"].Visible = false;
            this.dgvca.Columns["TINHVESOM"].Visible = false;
            this.dgvca.Columns["TINHDITRE"].Visible = false;
            this.dgvca.Columns["CHOPHEPVESOM"].Visible = false;
            this.dgvca.Columns["CHOPHEPDITRE"].Visible = false;
        }
        private void InitGrid()
        {
            DgvFilterManager f = new DgvFilterManager();
            f.DataGridView = (DataGridView)this.dgvca;
        }

        void ChooseData(object sender, int e)
        {
            if (flag == 2)
            {
                try
                {
                   
                    this.dgvca.EndEdit();
                    DataGridViewRow dr = this.dgvca.Rows[e];
                    txtmaca.Text = dr.Cells["MACA"].Value.ToString();
                    txttenca.Text = dr.Cells["TENCA"].Value.ToString();
                    txtgiovao.Text = dr.Cells["TIMEIN"].Value.ToString();
                    txtgiora.Text = dr.Cells["TIMEOUT"].Value.ToString();
                    txtgiovaotu.Text = dr.Cells["GIOVAOTU"].Value.ToString();
                    txtgiovaoden.Text = dr.Cells["GIOVAODEN"].Value.ToString();
                    txtgioratu.Text = dr.Cells["GIORATU"].Value.ToString();
                    txtgioraden.Text = dr.Cells["GIORADEN"].Value.ToString();
                    txtchopheptre.Text = dr.Cells["CHOPHEPDITRE"].Value.ToString();
                    txtchophepsom.Text = dr.Cells["CHOPHEPVESOM"].Value.ToString();
                    if (dr.Cells["TINHDITRE"].Value.ToString() == "USE")
                    {
                        cbditre.Checked = true;
                    }
                    else
                    {
                        cbditre.Checked = false;
                    }
                    if (dr.Cells["TINHVESOM"].Value.ToString() == "USE")
                    {
                        cbvesom.Checked = true;
                    }
                    else
                    {
                        cbvesom.Checked = false;
                    }
                   
                }
                catch { return; };
            }
        }

        private void dgvca_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            ChooseData(sender, e.RowIndex);
        }

        private void dgvca_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
        {
            ChooseData(sender, 0);
        }

        private void dgvca_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            ChooseData(sender, e.RowIndex);
        }

        private void Ca_Load(object sender, EventArgs e)
        {
            Event();
            SetLink();
        }
        private void SetLink()
        {
            this.links1.SetValues("Danh mục", "Ca làm việc ");
        }

    }
}
